package com.example.sqlitetest.View.widget;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

import com.example.sqlitetest.Model.SQLiteDao;
import com.example.sqlitetest.R;

public class NoteActivity extends AppCompatActivity {

	private ListView mListView;
	private Button   mBtnWrite;
    private Cursor   mCursor;
	private SQLiteDao mDao;
	private int _id = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_note);
		mDao = new SQLiteDao(this);
		mCursor = mDao.select();
		initViews();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
	}

	private void initViews() {
	    mListView = (ListView)findViewById(R.id.listview);
		mBtnWrite = (Button)findViewById(R.id.btn_write);
		SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.layout_view_item_notes,mCursor, new String[]{SQLiteDao.CONTENT},new int[]{R.id.tv_info});
        mListView.setAdapter(mAdapter);
		mListView.invalidateViews();
		mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				//查询方法
				mCursor.moveToPrevious();
				//如果没有查询到清零
                _id = mCursor.getInt(0);
                Intent intent = new Intent(NoteActivity.this, NotedifyActivity.class);
                intent.putExtra("id", _id);
				intent.putExtra("data", mCursor.getString(1));
				startActivity(intent);
				finish();
			}
		});
		mBtnWrite.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(NoteActivity.this, NoteEditActivity.class);
				startActivity(intent);
			}
		});
	}
}
